package com.example.stu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.stu.entity.PageResult;
import com.example.stu.entity.Result;
import com.example.stu.entity.res.StuSelectDormitoryVo;
import com.example.stu.mapper.DormitoryMapper;
import com.example.stu.mapper.DormitoryStudentDetailMapper;
import com.example.stu.mapper.MajorDormitoryMapper;
import com.example.stu.mapper.StudentMapper;
import com.example.stu.pojo.Dormitory;
import com.example.stu.pojo.DormitoryStudentDetail;
import com.example.stu.pojo.MajorDormitory;
import com.example.stu.pojo.Student;
import com.example.stu.service.DormitoryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Date: 2021/3/4
 * @Author: 黄先军
 * @Description:
 */
@Service
public class DormitoryServiceImpl implements DormitoryService {

    @Resource
    private DormitoryMapper dormitoryMapper;

    @Resource
    private MajorDormitoryMapper majorDormitoryMapper;

    @Resource
    private StudentMapper studentMapper;

    @Resource
    private DormitoryStudentDetailMapper dormitoryStudentDetailMapper;
    /**
     * 通过学生学号查找对应寝室数据【剩余床位什么的】
     * @param id
     * @return
     */
    @Override
    public PageResult findDormitoryByStuId(Integer id) {
        if(id == null){
            return null;
        }
        //查询对应学生的专业
        Student student = studentMapper.selectOne(new LambdaQueryWrapper<Student>()
                .eq(Student::getStuId, id)
                .select(Student::getMajorId));
        //根据学生的专业查询对应的寝室的id
        List<Integer> dormitoryListId = majorDormitoryMapper.selectList(new LambdaQueryWrapper<MajorDormitory>()
                .eq(MajorDormitory::getMajorId, student.getMajorId())
                .select(MajorDormitory::getDormitoryId))
                .parallelStream()
                .map(MajorDormitory::getDormitoryId)
                .collect(Collectors.toList());
        //根据对应的寝室id查询出寝室的信息
        List<Dormitory> dormitories = dormitoryMapper.selectList(new LambdaQueryWrapper<Dormitory>()
                .in(Dormitory::getId, dormitoryListId));
        int size = dormitories.size();
        return new PageResult((long)size,dormitories);
    }

    /**
     * 学生选择寝室入住
     * @param stuSelectDormitoryVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Result selectDormitory(StuSelectDormitoryVo stuSelectDormitoryVo) {
        if(stuSelectDormitoryVo==null){
            return new Result(false,"系统错误，请联系管理员");
        }
        //寝室号
        String dormitoryId = stuSelectDormitoryVo.getDormitoryId();
        //学号
        String stuId = stuSelectDormitoryVo.getStuId();
        //判断寝室是否满员
        Dormitory dormitory = dormitoryMapper.selectOne(new LambdaQueryWrapper<Dormitory>()
                .eq(Dormitory::getId, Integer.parseInt(dormitoryId)));
        if(dormitory.getSurplusBed()<=0){
            return new Result(false,"该寝室已经住满了，请选择其他寝室");
        }
        //入住寝室，更新对应的student_info表
        Student student = studentMapper.selectOne(new LambdaQueryWrapper<Student>()
                .eq(Student::getStuId, stuId));
        //判断学生是否有寝室住了
        if(StringUtils.isNotBlank(student.getStuDormitory())){
            return new Result(false,"你已经有寝室住了，不能继续选择");
        }
        student.setStuDormitory(dormitory.getDormitoryName());
        studentMapper.updateById(student);
        //修改dormitory表[床位剩余数量]
        dormitory.setSurplusBed(dormitory.getSurplusBed()-1);
        dormitoryMapper.updateById(dormitory);
        //修改dormitory_detail表
        DormitoryStudentDetail dormitoryStudentDetail = new DormitoryStudentDetail();
        dormitoryStudentDetail.setDormitoryId(Integer.parseInt(dormitoryId));
        dormitoryStudentDetail.setDormitoryName(dormitory.getDormitoryName());
        dormitoryStudentDetail.setStudentId(Integer.parseInt(stuId));
        dormitoryStudentDetail.setStudentName(student.getStuName());
        dormitoryStudentDetailMapper.insert(dormitoryStudentDetail);
        return new Result(true,"成功入住该寝室");
    }
}
